package com.colin.base.deadlock;

import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;

/**
 * Created by Colin on 2020/3/22 21:03
 * email: colinzhaodong@gmail.com
 * desc: //ThreadMXBean检查死锁
 *
 * @author zhaod
 */
public class ThreadMXBeanDetection implements Runnable{
	int flag = 1;

	static Object o1 = new Object();
	static Object o2 = new Object();

	public static void main(String[] args) throws InterruptedException {
		ThreadMXBeanDetection r1 = new ThreadMXBeanDetection();
		ThreadMXBeanDetection r2 = new ThreadMXBeanDetection();
		r1.flag = 1;
		r2.flag = 0;
		Thread t1 = new Thread(r1);
		Thread t2 = new Thread(r2);
		t1.start();
		t2.start();
		Thread.sleep(1000);
		ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
		long[] deadlockedThreads = threadMXBean.findDeadlockedThreads();
		if (deadlockedThreads != null && deadlockedThreads.length > 0) {
			for (int i = 0; i < deadlockedThreads.length; i++) {
				ThreadInfo threadInfo = threadMXBean.getThreadInfo(deadlockedThreads[i]);
				System.out.println("发现死锁" + threadInfo.getThreadName());
			}
		}
	}

	@Override
	public void run() {
		System.out.println("flag = " + flag);
		if (flag == 1) {
			synchronized (o1) {
				try {
					Thread.sleep(500);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				synchronized (o2) {
					System.out.println("线程1成功拿到两把锁");
				}
			}
		}
		if (flag == 0) {
			synchronized (o2) {
				try {
					Thread.sleep(500);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				synchronized (o1) {
					System.out.println("线程2成功拿到两把锁");
				}
			}
		}
	}
}
